Skip to content

Conversation

@tyralla
Copy link
Collaborator

@tyralla tyralla commented Nov 29, 2024

Fixes #16379

Only a modest change. PR #18180 brought me to issue #18238. Trying to fix the latter revealed inconsistencies between join-style and union-style narrowing, so I decided to deal with this issue first. And here is the (maybe too simple?) solution...

Regarding the primer output: For pandas, a (possibly debatable) false negative turns into a similar one. The affected pydantic and pymungo functions are complicated and not stringently typed. I would say the new error messages are okay, but I am not 100% sure.

tyralla and others added 2 commits November 29, 2024 17:41
…ogic.

More concretely: use `make_simplified_union` instead of `join_simple` in `ConditionalTypeBinder.update_from_options`
@tyralla
Copy link
Collaborator Author

tyralla commented Nov 29, 2024

Just a draft, I did not find the button to mark it accordingly...

@github-actions

This comment has been minimized.

…ing_towards_union_based_narrowing' into narrowing/move_join_based_narrowing_towards_union_based_narrowing
@github-actions

This comment has been minimized.

@tyralla tyralla marked this pull request as draft December 1, 2024 11:54
@tyralla tyralla added topic-type-narrowing Conditional type narrowing / binder topic-join-v-union Using join vs. using unions labels Dec 1, 2024
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 2, 2024

Diff from mypy_primer, showing the effect of this PR on open source code:

pydantic (https://github.com/pydantic/pydantic)
+ pydantic/v1/main.py:901: error: Set comprehension has incompatible type Set[int | str]; expected Set[str | None]  [misc]
+ pydantic/v1/main.py:901: note: Left operand is of type "set[str] | dict_keys[str, Any]"

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/computation/expr.py:680: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/core/computation/expr.py:680: error: Item "Attribute" of "Attribute | Name" has no attribute "id"  [union-attr]
+ pandas/core/computation/expr.py:680: note: Error code "union-attr" not covered by "type: ignore" comment
+ pandas/core/computation/expr.py:702: error: Unused "type: ignore" comment  [unused-ignore]
+ pandas/core/computation/expr.py:702: error: Item "Attribute" of "Attribute | Name" has no attribute "id"  [union-attr]
+ pandas/core/computation/expr.py:702: note: Error code "union-attr" not covered by "type: ignore" comment

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ pymongo/synchronous/cursor.py:1002: error: Invalid index type "int | Any | Pattern[Any]" for "list[Any]"; expected type "SupportsIndex"  [index]
+ pymongo/asynchronous/cursor.py:1004: error: Invalid index type "int | Any | Pattern[Any]" for "list[Any]"; expected type "SupportsIndex"  [index]

@tyralla tyralla requested review from JukkaL and ilevkivskyi December 3, 2024 21:38
@tyralla tyralla marked this pull request as ready for review December 3, 2024 21:39
Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this approach, it is way too ad hoc. A more principled solution would be to not use joins at all in update_from_options(). But this will likely cause a bigger fallout, and will also require adjusting some edge cases.

This would be more consistent with ternary expressions, and IIRC this is the last place join_simple() is used. Ideally join/meet should be only used by the constraint solver.

@ilevkivskyi
Copy link
Member

Ideally join/meet should be only used by the constraint solver.

Btw I just checked and there are very few left (apart from the one being discussed), couple ad-hoc uses in checkexpr.py, and few more in checkpattern.py.

@tyralla
Copy link
Collaborator Author

tyralla commented Dec 4, 2024

If "move towards..." is not "principled" enough, I better close this PR. I could try a complete step later (and learn a little about the edge cases of narrowing), but please do not hesitate to do it yourself if you have the time and interest (or if you expect less work when implementing than when reviewing it).

@tyralla tyralla closed this Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic-join-v-union Using join vs. using unions topic-type-narrowing Conditional type narrowing / binder

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect type narrowing for select class hierarchies

2 participants